<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2020/8/27
  Time: 23:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
</head>

<body>
<!--表单查询-->
<div class="panel panel-primary" style="border: 1px solid dodgerblue;">
    <div class="panel-heading" style="background-color: dodgerblue;">条件查询</div>
    <br>
    <form class="form-horizontal">
        <div class="form-group">

            <label class="col-sm-2 control-label" align="right">用户姓名:</label>
            <div class="col-sm-2">
                <input type="text" class="form-control" placeholder="请输入要查询的姓名" />
            </div>

            <div class="col-sm-4">
                <button class="btn btn-success btn-sm">查询</button>
                <button type="button" class="btn btn-success btn-sm" onclick="addWin()">新增</button>
            </div>
        </div>
    </form>
</div>
<!--表格-->
<div class="panel panel-primary" style="border: 1px solid dodgerblue;">
    <div class="panel-heading" style="background-color: dodgerblue;">用户列表</div>
    <table class="table table-bordered table-hover">
        <thead>
        <th width="5%">id</th>
        <th>用户姓名</th>
        <th>账号</th>
        <th>密码</th>
        <th>角色</th>
        <th width="20%">操作</th>
        </thead>
        <tbody id="tb">

        </tbody>
    </table>

</div>
<div style="position: absolute;left: 40%;">
    每页
    <select style="height: 30px;width: 60px; border: 1px solid green;" id="myrow" onchange="loadList()">
        <option value="5">5</option>
        <option value="10">10</option>
    </select>
    条

    <button class="btn btn-success btn-sm" onclick="pre()">上一页</button>
    <button class="btn btn-success btn-sm" onclick="next()">下一页</button>
    <button class="btn btn-success btn-sm" onclick="indexPage()">首页</button>
    <button class="btn btn-success btn-sm" onclick="endPages()">尾页</button>
    共<span id="total">xx</span>页
    一共<span id="pages">xx</span>条
    当前<span id="curPage">xx</span>页
</div>
<!--模态框-->
<div class="modal fade" id="myWin" style="top:100px">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="background-color: dodgerblue;height: 40px;padding-top: 1px;">
                <h4 align="center" id="title">用户新增</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label class="col-sm-2 control-label" align="right">id:</label>
                        <div class="col-sm-10">
                            <input type="text" id="myId" class="form-control" placeholder="请输入id" />
                        </div>

                        <label class="col-sm-2 control-label" align="right">姓名:</label>
                        <div class="col-sm-10">
                            <input type="text" id="uname" class="form-control" placeholder="请输入姓名" />
                        </div>

                        <label class="col-sm-2 control-label" align="right">账号:</label>
                        <div class="col-sm-10">
                            <input type="text" id="userName" class="form-control" placeholder="请输入账号" />
                        </div>

                        <label class="col-sm-2 control-label" align="right">密码:</label>
                        <div class="col-sm-10">
                            <input type="text" id="userPwd" class="form-control" placeholder="请输入密码" />
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-success" type="button" id="updateBtn"onclick="update()">修改</button>
                <button class="btn btn-success" type="button" id="addBtn" onclick="insert()">新增</button>
                <button class="btn btn-success" type="button" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!--模态框-->
<div class="modal fade" id="fpWin" style="top:100px">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="background-color: dodgerblue;height: 40px;padding-top: 1px;">
                <h4 align="center" id="title1">分配权限</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="form-group">

                            <input type="hidden" id="uId" class="form-control" placeholder="请输入id" />

                        <label class="col-sm-2 control-label" align="right">角色:</label>
                        <div class="col-sm-10">
                            <select id="role">
                                <option value="2">班主任</option>
                                <option value="3">教师</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">

                <button class="btn btn-success" type="button" id="fpBtn" onclick="fp()">分配</button>
                <button class="btn btn-success" type="button" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!--模态框-->
<div class="modal fade" id="roleWin" style="top:100px">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="background-color: dodgerblue;height: 40px;padding-top: 1px;">
                <h4 align="center" id="title2">角色信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="form-group">

                        <input type="hidden" id="userId" class="form-control" placeholder="请输入id" />


                        班主任：<input type="checkbox" id="ban" value="2">
                        教师：<input type="checkbox" id="jiao" value="3">
                    </div>
                </form>
            </div>
            <div class="modal-footer">

                <button class="btn btn-success" type="button" id="xgBtn" onclick="xg()">修改</button>
                <button class="btn btn-success" type="button" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script>
    //加载事件
    $(function () {
        loadList();
    })

    var page=1;//页码
    var endPage;//尾页
    function pre() {//上一页
        page--;
        if (page<1){
            page=1;
        }
        loadList();
    }

    function next() {//下一页
        page++;
        if (page>endPage){
            page=endPage;
        }
        loadList();
    }
    function indexPage() {//首页
        page=1;
        loadList();
    }
    function endPages() {//尾页
        page=endPage;
        loadList();
    }
    function loadList() {//显示列表
        //获取用户选择的每页显示条数
        var row = $("#myrow").val();
        $.ajax({
            url:"${pageContext.request.contextPath}/user/select",
            data:{"page":page,"row":row},
            type:"get",
            dataType:"json",
            success:function(data){
                //获取查询的结果集
                var list = data.data;
                //尾页
                endPage=data.endPage;
                //显示总条数
                $("#pages").text(data.total);
                //显示总页数
                $("#total").text(data.pages);
                //显示当前页
                $("#curPage").text(data.curPage);
                //拼接html的变量
                var html="";
                for(var i=0;i<list.length;i++){
                    html+="<tr>" +
                        "<td>"+list[i].uId+"</td>" +
                        "<td>"+list[i].name+"</td>" +
                        "<td>"+list[i].userName+"</td>" +
                        "<td>"+list[i].userPwd+"</td>" +
                        "<td><button class='btn btn-warning btn-sm' onclick='selectRole(this)'>角色信息</button></td>" +
                        "<td><button class='btn btn-warning btn-sm' onclick='upWindow(this)'>修改</button>" +
                        "<button class='btn btn-danger btn-sm' onclick='del(this)'>删除</button>" +
                        "<button class='btn btn-warning btn-sm' onclick='fpRole(this)'>分配权限</button></td>"+
                        "</tr>"
                }
                //html设置文本和标签
                $("#tb").html(html);

            }
        })
    }
    function selectRole(obj) {
        $("#roleWin").modal("show");
//获取选中行的的第一列的数据
        var username = $(obj).parent().parent().find("td").eq(2).text();
        var userId = $(obj).parent().parent().find("td").eq(0).text();
        //设置模态框中 的值
        $("#userId").val(userId);

        $("#ban").prop("checked",false);
        $("#jiao").prop("checked",false);
        $.ajax({
            url:"${pageContext.request.contextPath}/user/selectRole",
            data:{"userName":username},
            type:"get",
            dataType:"json",
            success:function(data) {

                var listrole = data.x;
                for (var i = 0; i < listrole.length; i++) {
                    if (listrole[i] == "班主任") {
                        $("#ban").prop("checked", true);
                    } else if (listrole[i] == "任课教师") {
                        $("#jiao").prop("checked", true);

                    }

                } loadList();
            }
        })
    }

    function xg() {
    var uid=$("#userId").val();
        for (var i = 0; i <2; i++) {
            if ($("#ban").prop("checked")&&i==0){
                $.ajax({
                    url:"${pageContext.request.contextPath}/user/xgRole",
                    data:{"uId":uid,"rId":2},
                    type:"get",
                    dataType:"json",
                    success:function(data){
                        if(data.info=="修改成功"){
                            $("#roleWin").modal("hide");

                            //刷新
                            loadList();
                        }

                    }
                })
            }
            if ($("#jiao").prop("checked")&&i==1){
                $.ajax({
                    url:"${pageContext.request.contextPath}/user/xgRole",
                    data:{"uId":uid,"rId":3},
                    type:"get",
                    dataType:"json",
                    success:function(data){
                        if(data.info=="修改成功"){
                            $("#roleWin").modal("hide");

                            //刷新
                            loadList();
                        }else{
                            alert(data.info);
                        }

                    }
                })
            }
        }
    }
function fpRole(obj) {
$("#fpWin").modal("show");
//获取选中行的的第一列的数据
    var id = $(obj).parent().parent().find("td").eq(0).text();
    //设置模态框中 的值
    $("#uId").val(id);
}
function fp() {
    var rid = $("#role").val();
    var uid=  $("#uId").val();
    $.ajax({
        url:"${pageContext.request.contextPath}/user/fpRole",
        data:{"uId":uid,"rId":rid},
        type:"get",
        dataType:"json",
        success:function(data){
            if(data.info=="分配成功"){
                $("#fpWin").modal("hide");

                //刷新
                loadList();
            }else{
                alert(data.info);
            }

        }
    })
}

    //打开新增窗口
    function addWin(){
        //清空模态框的数据
        clearWin();
        //显示模态框
        $("#myWin").modal("show");
        //更改窗体的标题
        $("#title").text("用户新增");
        //隐藏修改按钮的button
        $("#updateBtn").hide();
        //显示新增按钮的button
        $("#addBtn").show();

    }

    //新增数据
    function insert() {
        //获取员工姓名
        var name = $("#uname").val();
        var userName = $("#userName").val();
        var userPwd = $("#userPwd").val();


        $.ajax({
            url:"${pageContext.request.contextPath}/user/add",
            data:{"name":name,"userName":userName,"userPwd":userPwd},
            type:"get",
            dataType:"json",
            success:function(data){
                if(data.info=="新增成功"){
                    $("#myWin").modal("hide");

                    //刷新
                    loadList();
                }else{
                    alert(data.info);
                }

            }
        })
    }

    //清空
    function clearWin(){
        $("#myId").val("");
        $("#uname").val("");
        $("#userName").val("");
        $("#userPwd").val("");
    }

    //打开修改窗口
    function upWindow(obj){
        //显示模态框
        $("#myWin").modal("show");
        //更改窗体的标题
        $("#title").text("用户修改");
        //隐藏新增按钮的button
        $("#addBtn").hide();
        //显示修改按钮的button
        $("#updateBtn").show();

        //获取选中行的的第一列的数据
        var id = $(obj).parent().parent().find("td").eq(0).text();
        //设置模态框中 的值
        $("#myId").val(id);

        //获取选中行的的第二列的数据
        var name = $(obj).parent().parent().find("td").eq(1).text();
        //设置模态框中 的值
        $("#uname").val(name);

        //获取选中行的的第三列的数据
        var username = $(obj).parent().parent().find("td").eq(2).text();
        //设置模态框中 的值
        $("#userName").val(username);

        //获取选中行的的第四列的数据
        var pwd = $(obj).parent().parent().find("td").eq(3).text();
        //设置模态框中 的值
        $("#userPwd").val(pwd);

    }

    //修改数据
    function update() {
        //获取id
        var userId = $("#myId").val();
        //获取员工姓名
        var Name = $("#uname").val();
        var userName = $("#userName").val();
        //获取密码
        var pwd = $("#userPwd").val();
        $.ajax({
            url:"${pageContext.request.contextPath}/user/update",
            data:{"uId":userId,"name":Name,"userName":userName,"userPwd":pwd},
            type:"get",
            dataType:"json",
            success:function(data){
                if(data.info=="修改成功"){
                    $("#myWin").modal("hide")
                    //刷新
                    loadList();
                }else{
                    alert(data.info);
                }

            }
        })
    }

    //删除
    function del(obj) {
        var info = confirm("你确定要删除吗")
        if(info){
            //获取要删除的id
            var id = $(obj).parent().parent().find("td").eq(0).text();
            $.ajax({
                url:"${pageContext.request.contextPath}/user/del",
                data:{"id":id},
                type:"get",
                dataType:"json",
                success:function(data){
                    if(data.info=="删除成功"){
                        $("#myWin").modal("hide")
                        //刷新
                        loadList();
                    }else{
                        alert(data.info);
                    }

                }
            })
        }
    }

</script>
